<template>
  <div class="change-pic">
    <h3>更换头像</h3>
    <div class="main">
      <img :src="imageUrl" alt="">
      <input type="file" ref="ipt">
      <div class="btn">
        <el-button type="primary" @click="onfiles">上传头像</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { updatePic } from '@/api/user'
export default {
  data () {
    return {
      imageUrl: null
    }
  },
  methods: {
    onfiles () {
      const filereader = new FileReader()
      filereader.readAsDataURL(this.$refs.ipt.files[0])
      filereader.onload = async s => {
        this.imageUrl = s.target.result
        await updatePic(s.target.result)
        this.$message.success('上传成功')
      }
    }
  }
}

</script>

<style lang="less" scoped>
.change-pic {
  h3 {
    padding: 0px;
    margin-bottom: 30px;
  }
  img {
    width: 200px;
    height: 200px;
  }
}
</style>
